#!/usr/bin/perl
# index.cgi
# Logical Volume Management 
require './lvm-lib.pl';
&ReadParse();

$config{'mkfslock'} = "/var/nas/lock/mkfsd_new";
$config{'rsfslock'} = "/var/nas/lock/resizefsd";
if( -e $config{'mkfslock'}) {
	&redirect("volstat.cgi");
}
elsif(-e $config{'rsfslock'}){
	&redirect("rslvstat.cgi");
}
else{   #-----------No LV Formating--------------#
&header($text{'index_title'}, "", "index", 0, 1, undef, undef, "<meta http-equiv=refresh content=30>");

my @pvinfo = &get_pv_info();
my @vginfo = &get_vg_info();
my @lvinfo = &get_lv_info();
print "<form name=lvm action=index.cgi method=post onsubmit='return onsubmit_form()'>\n";
print "<table width=100% class=mode2>\n";
print "<tr><td valign=top><table class=mode1 border=0 cellspacing=0 cellpadding=1 width=100%>\n";
print "<tr class=title1><th align=left>$text{'index_pv'}</th>",
	"<th align=left>$text{'index_pv_vg'}</th>",
	"<th align=left>$text{'index_pv_raid'}</th>",
	"<th align=left>$text{'index_pv_size'}</th>",
	"<th align=left>$text{'index_pv_free'}</th></tr>\n";
	#"<th align=left>$text{'index_pv_status'}</th> </tr>\n";
$i = 0;
#$j = @pvinfo;print $j;
foreach $pv (@pvinfo) {
	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}
	my $disable = '';
	my $onclick = " "; 
	print "<tr $dismode>\n";
	print "<td nowarp $dismode width=20%>";
	print "<input type=radio name=pv value=$pv->{'pv'}>";
	print "$pv->{'pv'}</td>";
	print "<td>$pv->{'vg'}</td>";
	print "<td>$pv->{'raid'}</td>";
	print "<td>$pv->{'size'}</td>";
	print "<td>$pv->{'free'}</td>";
	print "<td>$pv->{'status'}</td></tr>";
}
print "</table></td>\n";

print "<td valign=top width=60>"; 
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
print "<tr><td align=center><input type=submit name=pvrescan value=$text{'bt_pv_rescan'} class=button onclick='return SetEditValue_pv(this, $i)'></td></tr>";
print "<tr><td align=center><input type=submit name=pvresize value=$text{'bt_pvresize'} class=button onclick='return SetEditValue_pv(this, $i)'></td></tr>\n";
print "<tr><td align=center><input type=submit name=vgcreate value=$text{'bt_vg_create'} class=button onclick='return SetEditValue_pv(this, $i)'></td></tr>";
print "</table></td></tr></table>";

print "<table width=100% class=mode2><tr> <td colpan=6> <hr> </td></tr></table>\n";

#-------------VG infomation-------------#
print "<table width=100% class=mode2>\n";
print "<tr><td valign=top><table class=mode1 vorder=0 cellspacing=0 cellpadding=1 width=100%>\n";
print "<tr class=title1><th align=left>$text{'index_vg'}</th>",
	"<th align=left>$text{'index_vg_status'}</th>",
	"<th align=left>$text{'index_vg_pvnum'}</th>",
	"<th align=left>$text{'index_vg_lvnum'}</th>",
	"<th align=left>$text{'index_vg_size'}</th>",
	"<th align=left>$text{'index_vg_free'}</th></tr>\n";
$i = 0;
foreach $vg (@vginfo) {
	$i++;
	if($i % 2 ==0){
		$dismode = "class=mode2";
	}else{
		$dismode = "class=mode1";
	}
	print "<tr $dismode><td nowarp $dismode width=20%>";
	print "<input type=radio name=vg value=$vg->{'vg'} $disable $onclick>";
	print "$vg->{'vg'}</td>\n";
	print "<td>$vg->{'status'}</td>\n";
	print "<td>$vg->{'pvnum'}</td>\n";
	print "<td>$vg->{'lvnum'}</td>\n";
	print "<td>$vg->{'size'}</td>\n";
	print "<td>$vg->{'free'}</td>\n";
	print "</tr>\n";
}
print "</table></td>";

print "<td valign=top width=60>";
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
print "<tr><td><input type=submit name=vginfo value=$text{'bt_vg_info'} class=button onclick='return SetEditValue_vg(this, $i)'></td></tr>";
print "<tr><td><input type=submit name=vgconfig value=$text{'bt_vg_config'} class=button onclick='return SetEditValue_vg(this, $i)'></td></tr>\n";
print "<tr><td><input type=submit name=vgdelete value=$text{'bt_vg_delete'} class=button onclick='return SetEditValue_vg(this, $i)'></td></tr>\n";
#print "<tr><td><input type=submit name= value=$text class=button></td></tr>\n";
print "<tr><td><input type=submit name=lvcreate value=$text{'bt_vg_createlv'} class=button onclick='return SetEditValue_vg(this,$i)'></td></tr>\n";
print "</table></td></tr></table>\n";

print "<table width=100% class=mode2><tr><td colpan=5><hr></td></tr></table>\n";
#-------------LV infomation-------------#
print "<table width=100% class=mode2>\n";
print "<tr><td valign=top><table class=mode1 vorder=0 cellspacing=0 cellpadding=1 width=100%>\n";

print "<tr class=title1><th align=left>$text{'index_lv'}</th>",
        "<th align=left>$text{'index_lv_status'}</th>",
        "<th align=left>$text{'index_lv_vg'}</th>",
        "<th align=left>$text{'index_lv_size'}</th>",
        "<th align=left>$text{'index_lv_fsmount'}</th>",
        "<th align=left>$text{'index_lv_usage'}</th></tr>\n";
$i = 0;
my $usage;
foreach $lv (@lvinfo) {
        $i++;
        if($i % 2 ==0){
                $dismode = "class=mode2";
        }else{
                $dismode = "class=mode1";
        }
        print "<tr $dismode><td nowarp $dismode width=20%>";
	print "<input type=radio name=lv value=$lv->{'lv'} $disable onclick='SetLvValue($lv->{'status_m'}, $lv->{'status_i'})'>";	
	print "$lv->{'lvname'}</td>\n";
        print "<td>$lv->{'status_a'}</td>\n";
        print "<td>$lv->{'vg'}</td>\n";
        print "<td>$lv->{'size'}</td>\n";
	#my $istat = &check_istat($lv->{'lv'});
	my $fstype = get_fstype($lv->{'lv'});
	if($lv->{'status_m'}){
	
        	print "<td>$text{'normal'} $fstype</td>\n";
		$usage = usagetext($lv->{'usage'}, $lv->{'freesize'});
		print "<td>$usage</td>\n";
	}
	elsif($lv->{'status_i'}){
                print "<td>iSCSI Device</td>";
                print "<td>iSCSI Device</td>\n";		
	}
	else{
		print "<td>$text{'na'}</td>\n";
		print "<td>$text{'na'}</td>";
	}
        #print "<td>free</td>\n";
        print "</tr>\n";
}
print "</table></td>";

print "<td valign=top width=60>";
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6>\n";
print "<tr><td><input type=hidden name=lvinfo value=$text{'bt_lv_info'} class=button onclick='return SetEditValue_lv(this, $i)'></td></tr>";
print "<tr><td><input type=submit name=lvformat value=$text{'bt_lv_format'} class=button onclick='return SetEditValue_lv(this, $i)' disabled=true></td></tr>";
print "<tr><td><input type=submit name=iscsi value=iSCSI class=button onclick='return SetEditValue_lv(this, $i)' disabled=true></td></tr>\n";
print "<tr><td><input type=submit name=lvconfig value=$text{'bt_lv_config'} class=button onclick='return SetEditValue_lv(this, $i)'></td></tr>";
print "<tr><td><input type=submit name=snapshot value=$text{'bt_snapshot'} class=button onclick='return SetEditValue_lv(this, $i)' disabled=true></td></tr>\n";
print "<tr><td><input type=submit name=lvdelete value=$text{'bt_lv_delete'} class=button onclick='return SetEditValue_lv(this, $i)'></td></tr>";
#print "<tr><td><input type=submit name= value= class=button></td></tr>";
print "</table></td></tr></table>\n";
############################################################
#print "<input type=hidden name=abc>";
print <<EOF;
<script>

function onsubmit_form()
{
//	alert('abc');
}

function GetSelect_pv(obj,n)
{
        var j;
        var findpv = false;
        if (n > 1) {
                for (j = 0; j < document.lvm.pv.length; j++) {
                        if (document.lvm.pv[j].checked) {
                                findpv = true;
                                break;
                        }
                }
        }
        else if (n == 1) {
                if (document.lvm.pv.checked) {
                        findpv = true;
                }
        }
        return findpv;
}

function SetEditValue_pv(obj, n)
{
	if(!GetSelect_pv(obj,n)){
		if (obj == document.lvm.pvresize){
			alert("$text{'noselectpv'}");
			return false;
		}
	}
	if (obj == document.lvm.pvrescan) {
		document.lvm.action = 'pvrescan.cgi';
	}
	if (obj == document.lvm.vgcreate) {
		document.lvm.action = 'vgcreate.cgi';
	}
	if (obj == document.lvm.pvresize) {
		document.lvm.action = 'pvresize.cgi';
	}
}


function GetSelect_vg(obj,n)
{
	var i;
	var found = false;
	if (n > 1) {
		for (i = 0; i < document.lvm.vg.length; i++) {
			if (document.lvm.vg[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.lvm.vg.checked) {
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.lvm.vg.value = document.lvm.vg[i].value;
		else
			document.lvm.vg.value = document.lvm.vg.value;
	}
	else
		document.lvm.vg.value = '';

	return found;

}

function SetEditValue_vg(obj,n)
{
	if(GetSelect_vg(obj,n)) {
		if(obj == document.lvm.vgdelete) {
			if(!confirm("$text{'deletevgconfirm'}"))
				return false;
		}
	}
	else{
		alert("$text{'noselectvg'}");
		return false;
	}
	if (obj == document.lvm.vginfo) {
		document.lvm.action = 'vginfo.cgi';
	}
	else if (obj == document.lvm.vgconfig) {
		document.lvm.action = 'vgconfig.cgi';
	}
	else if (obj == document.lvm.vgdelete) {
		document.lvm.action = 'vgdelete.cgi';
	}
	else if (obj == document.lvm.lvcreate) {
		document.lvm.action = 'lvcreate.cgi';
	}
	
}

function GetSelect_lv(obj,n)
{
	var j;
	var findlv = false;
	if (n > 1) {
		for (j = 0; j < document.lvm.lv.length; j++) {
			if (document.lvm.lv[j].checked) {
				findlv = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.lvm.lv.checked) {
			findlv = true;
		}
	}
	return findlv;
}

function SetEditValue_lv(obj,n)
{
	if(GetSelect_lv(obj,n)){
                if(obj == document.lvm.lvdelete) {
                        if(!confirm("$text{'deletelvconfirm'}"))
                                return false;
                }
                if(obj == document.lvm.lvformat) {
                        if(!confirm("$text{'formatlvconfirm'}"))
                                return false;
                }		
	}
	else{
		alert("$text{'err_noselectlv'}");
		return false;
	} 
	if (obj == document.lvm.lvinfo) {
		document.lvm.action = 'lvinfo.cgi';
	}
	else if (obj == document.lvm.lvformat) {
		//document.lvm.action = 'lvformat.cgi';
		document.lvm.action = 'choosefs.cgi';
	}
	else if (obj == document.lvm.lvconfig) {
		document.lvm.action = 'lvconfig.cgi';
	}
	else if (obj == document.lvm.lvdelete) {
		document.lvm.action = 'lvdelete.cgi';
	}
	else if (obj == document.lvm.iscsi) {
		document.lvm.action = '../tgtman/target_crtb.cgi';
	}
	else if (obj == document.lvm.snapshot) {
		document.lvm.action = 'snapshot.cgi';
	}
}
function SetLvValue(m, n)
{
	if (m == 0 && n==0) {
		document.lvm.lvformat.disabled = false;
		document.lvm.iscsi.disabled = false;
		document.lvm.snapshot.disabled = true;	
	}
	else {
		document.lvm.lvformat.disabled = true;
		document.lvm.iscsi.disabled = true;
		if (n==1) {
			document.lvm.lvconfig.disabled = true;
			document.lvm.snapshot.disabled =true;
		}
		else {
			document.lvm.lvconfig.disabled = false;
		}
		if (m == 1) {
			document.lvm.snapshot.disabled = false;
		}
	}
}
</script>

EOF

print "</form>";
&footer("/", $text{'index'});

} #-------------------No LV Formating--------------#
